//
//  YXGeneralSettingsViewController.m
//  MockProject
//
//  Created by 周丽丽 on 16/8/24.
//  Copyright © 2016年 niuzhaowang. All rights reserved.
//

#import "YXGeneralSettingsViewController.h"
#import "YXSettingModel.h"
#import "YXDownloadManger.h"
#define setDownloadViewH self.tabBarController.view.height
#define setDownloadViewW self.tabBarController.view.width
@interface YXGeneralSettingsViewController ()<UITableViewDelegate,
                                              UITableViewDataSource,
                                              UIPickerViewDelegate,
                                              UIPickerViewDataSource>
@property (nonatomic, strong) UITableView *settingTableView;
@property (nonatomic, strong) NSArray *generalsettingModelsArr;
@property (nonatomic, strong) UIView *cellAccessoryView;
@property (nonatomic, strong) UILabel *maxDownCountLabel;
@property (nonatomic, strong) UIButton *auxiliaryButton;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickDataArr;
@property (nonatomic, strong) UIView *maxCountView;
@property (nonatomic, strong) UILabel *pickLabel;
@end
@implementation YXGeneralSettingsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通用设置";
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"fcfcfc"];
    [self setupBackButton];
    UITableView *settingTableV = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:settingTableV];
    settingTableV.delegate = self;
    settingTableV.dataSource = self;
    [self settingData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.generalsettingModelsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *settingCellID =@"settingCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settingCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    YXSettingModel *model = self.generalsettingModelsArr[indexPath.row];
    cell.textLabel.text = model.title;
    if (indexPath.row == 0) {
        UISwitch *cellSwitch = [[UISwitch alloc]init];
        cellSwitch.on = YES;
        cellSwitch.onTintColor = [UIColor colorWithHexString:@"ff7000"];
        cell.accessoryView = cellSwitch;
        [cellSwitch addTarget:self action:@selector(cellSwitchIsChanged:) forControlEvents:UIControlEventValueChanged];
    }else if (indexPath.row == 1){
        [self setupCellAccessoryView:cell model:model];
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        [self setupMaxCountView];
        [self setupCoverView];
        [self.pickerView selectRow:self.maxDownCountLabel.text.intValue - 1 inComponent:0 animated:YES];
        [self pickerView:self.pickerView didSelectRow:self.maxDownCountLabel.text.intValue - 1 inComponent:0];
    }
}
- (void)setupCellAccessoryView:(UITableViewCell *)cell model:(YXSettingModel *)model{
    UIView *cellAccessoryV = [[UIView alloc]init];
    [cell.contentView addSubview:cellAccessoryV];
    self.cellAccessoryView = cellAccessoryV;
    
    UILabel *maxDownCountLabel = [[UILabel alloc]init];
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSInteger maxDownloadCountValue = [defaults integerForKey:kMaxDownloadCount];
    maxDownCountLabel.text = [NSString stringWithFormat:@"%zd",maxDownloadCountValue];
    maxDownCountLabel.font = [UIFont systemFontOfSize:16];
    maxDownCountLabel.textColor = [UIColor colorWithHexString:@"ff4600"];
    maxDownCountLabel.textAlignment = NSTextAlignmentRight;
    [self.cellAccessoryView addSubview:maxDownCountLabel];
    self.maxDownCountLabel = maxDownCountLabel;
    
    UIButton *auxiliaryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [auxiliaryButton setImage:[UIImage imageNamed:model.auxiliaryImg] forState:UIControlStateNormal];
    [auxiliaryButton setImage:[UIImage imageNamed:model.auxiliaryImgH] forState:UIControlStateHighlighted];
    [self.cellAccessoryView addSubview:auxiliaryButton];
    self.auxiliaryButton = auxiliaryButton;
    
    [self.cellAccessoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView.mas_centerX);
        make.right.top.bottom.equalTo(cell.contentView);
    }];
    [self.maxDownCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.cellAccessoryView);
        make.left.equalTo(self.cellAccessoryView);
        make.right.equalTo(self.auxiliaryButton.mas_left).offset(-6);
    }];
    [self.auxiliaryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cellAccessoryView).offset(-10);
        make.centerY.equalTo(self.cellAccessoryView);
    }];
}
- (void)setupCoverView{
    UIView *coverV = [[UIView alloc]init];
    coverV.backgroundColor = [UIColor blackColor];
    coverV.alpha = 0.2;
    [self.maxCountView addSubview:coverV];
    self.coverView = coverV;
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.maxCountView.mas_top);
    }];
}
- (void)setupMaxCountView{
    UIView *maxCountView = [[UIView alloc]init];
    [self.view addSubview:maxCountView];
    self.maxCountView = maxCountView;
    [self.maxCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.mas_equalTo(245 * AutoSizeScaleX);
    }];
    
    UIView *selectView = [[UIView alloc]init];
    [self.maxCountView addSubview:selectView];
    [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.maxCountView);
        make.height.mas_equalTo(40 * AutoSizeScaleX);
    }];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectView addSubview:cancelButton];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectView);
        make.left.equalTo(selectView).offset(39);
    }];
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectView addSubview:selectButton];
    [selectButton setTitle:@"选择" forState:UIControlStateNormal];
    [selectButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateNormal];
    [selectButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectView);
        make.right.equalTo(selectView).offset(-39);
    }];
    
    UIView *horizontalLineView = [[UIView alloc]init];
    horizontalLineView.backgroundColor = [UIColor colorWithHexString:@"e0dcd9"];
    [selectView addSubview:horizontalLineView];
    [horizontalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectView.mas_bottom);
        make.centerX.equalTo(selectView);
        make.width.mas_equalTo(690);
        make.height.mas_equalTo(1);
    }];
    
    UIPickerView *pickView = [[UIPickerView alloc]init];
    [self.maxCountView addSubview:pickView];
    self.pickerView = pickView;
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.maxCountView);
        make.top.equalTo(selectView.mas_bottom);
    }];
}
#pragma mark -PickerView for MaxDownloadCount
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.pickDataArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.pickDataArr objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.textColor = [UIColor colorWithHexString:@"403e3d"];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setFont:[UIFont systemFontOfSize:16]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *lable =(UILabel *) [pickerView viewForRow:row forComponent:component];
    lable.textColor = [UIColor colorWithHexString:@"ff4600"];
    self.pickLabel = lable;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}
- (void)cellSwitchIsChanged:(UISwitch *)sender{
    [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:kNetworkSwitchState];
}
- (void)cancelButtonClick:(UIButton *)sender{
    self.coverView.alpha = 0;
    self.coverView.hidden = YES;
    self.maxCountView.alpha = 0;
    self.maxCountView.hidden = YES;
}
- (void)selectButtonClick:(UIButton *)sender{
    self.maxDownCountLabel.text = self.pickLabel.text;
    self.maxCountView.alpha = 0;
    self.maxCountView.hidden = YES;
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setInteger:self.maxDownCountLabel.text.intValue forKey:kMaxDownloadCount];
    [defaults synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:kMaxDownloadCountChanged object:self];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"保存成功";
    hud.margin = 10.f;
    hud.yOffset = 150.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:3];
}
- (void)settingData{
    self.pickDataArr = @[
                         @"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"
                         ];
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Generalsetting.plist" ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[[YXSettingModel alloc]initWithDictionary:dict error:nil]];
    }
    self.generalsettingModelsArr = arrayM.copy;
}
- (void)setupBackButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn setImage:[UIImage imageNamed:@"返回按钮正常态"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"返回按钮-点击态"] forState:UIControlStateSelected];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
}
-(void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
